<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <button class="back-btn" @click="handleBack" aria-label="返回">
        <ChevronLeft :size="20" />
      </button>
      
      <div class="tab-wrapper">
        <TabNavigation 
          :tabs="tabData" 
          :activeIndex="activeTabIndex"
          @change="handleTabChange"
        />
      </div>
      
      <button class="edit-btn" @click="handleEdit" aria-label="编辑">
        <Edit3 :size="16" />
      </button>
    </header>

    <!-- 普通滑动轮播 -->
    <section class="swiper-carousel-section">
      <SwiperCarousel 
        :items="swiperImages"
        :autoPlay="true"
        :autoPlayInterval="5000"
        containerClass="custom-swiper-size"
        @slideChange="handleSwiperSlideChange"
        @itemClick="handleSwiperItemClick"
      />
    </section>

    <!-- 扇形轮播 -->
    <section class="carousel-section">
      <FanCarousel 
        ref="fanCarousel"
        :items="carouselImages"
        :autoPlay="false"
        :initialIndex="1"
        @slideChange="handleSlideChange"
        @itemClick="handleItemClick"
        @swiperItemClick="handleFanSwiperItemClick"
      />
    </section>

    <!-- 价格选择区域 -->
    <PriceSection 
      :defaultSelectedPrice="selectedPrice"
      @submit="handleSubmit"
      @priceChange="handlePriceChange"
    />
    
    <!-- 测试滚动内容 -->
    <div class="test-content">
      <div class="test-section">
        <h3>功能特色</h3>
        <ul>
          <li>智能美颜算法</li>
          <li>一键瘦脸瘦身</li>
          <li>肌肤质感优化</li>
          <li>眼部增大美化</li>
        </ul>
      </div>
      <div class="test-section">
        <h3>支持格式</h3>
        <ul>
          <li>JPG / JPEG</li>
          <li>PNG</li>
          <li>WebP</li>
          <li>HEIC</li>
        </ul>
      </div>
      <div class="test-section">
        <h3>服务保障</h3>
        <ul>
          <li>24小时在线客服</li>
          <li>不满意全额退款</li>
          <li>专业修图师团队</li>
          <li>隐私安全保护</li>
        </ul>
      </div>
    </div>
    
    <!-- 图片画廊 -->
    <ImageGallery 
      :visible="galleryVisible"
      :images="currentGalleryImages"
      :initialIndex="galleryInitialIndex"
      @close="handleGalleryClose"
      @change="handleGalleryChange"
    />
  </div>
</template>

<script>
import { ChevronLeft, Edit3 } from 'lucide-vue-next'
import TabNavigation from '@/components/TabNavigation.vue'
import FanCarousel from '@/components/carousel/FanCarousel.vue'
import SwiperCarousel from '@/components/carousel/SwiperCarousel.vue'
import PriceSection from '@/components/PriceSection.vue'
import ImageGallery from '@/components/ImageGallery.vue'

export default {
  name: 'BeautyApp',
  components: {
    ChevronLeft,
    Edit3,
    TabNavigation,
    FanCarousel,
    SwiperCarousel,
    PriceSection,
    ImageGallery
  },
  data() {
    return {
      activeTabIndex: 0,
      galleryVisible: false,
      galleryInitialIndex: 0,
      selectedPrice: 1, // 默认选择15元（索引1）
      currentGalleryImages: [], // 当前显示在画廊中的图片列表
      tabData: [
        { text: '人像美容' },
        { text: '改字抠图' },
        { text: '证件照' },
        { text: '风景修图' },
        { text: '商品拍摄' },
        { text: 'Logo设计' },
        { text: '海报制作' }
      ],
      carouselImages: [
        {
          title: '人像美容',
          items: [
            {
              beforeImage: require('@/assets/images/carousel/1before.jpg'),
              afterImage: require('@/assets/images/carousel/1after.jpg')
            }
          ]
        },
        {
          title: '风景处理',
          items: [
            {
              beforeImage: require('@/assets/images/carousel/2before.jpg'),
              afterImage: require('@/assets/images/carousel/2after.jpg')
            },
            {
              beforeImage: require('@/assets/images/carousel/2.1before.jpg'),
              afterImage: require('@/assets/images/carousel/2.1after.jpg')
            }
          ]
        },
        {
          title: '商品拍摄',
          items: [
            {
              beforeImage: require('@/assets/images/carousel/1.1before.jpg'),
              afterImage: require('@/assets/images/carousel/1.1after.jpg')
            },
            {
              beforeImage: require('@/assets/images/carousel/1.2before.jpg'),
              afterImage: require('@/assets/images/carousel/1.2after.jpg')
            },
            {
              beforeImage: require('@/assets/images/carousel/2.3before.jpg'),
              afterImage: require('@/assets/images/carousel/2.3after.jpg')
            }
          ]
        }
      ],
      // 为普通轮播单独准备的数据源
      swiperImages: [
        {
          beforeImage: require('@/assets/images/carousel/1before.jpg'),
          afterImage: require('@/assets/images/carousel/1after.jpg')
        },
        {
          beforeImage: require('@/assets/images/carousel/1.1before.jpg'),
          afterImage: require('@/assets/images/carousel/1.1after.jpg')
        },
        {
          beforeImage: require('@/assets/images/carousel/1.2before.jpg'),
          afterImage: require('@/assets/images/carousel/1.2after.jpg')
        },
        {
          beforeImage: require('@/assets/images/carousel/2before.jpg'),
          afterImage: require('@/assets/images/carousel/2after.jpg')
        },
        {
          beforeImage: require('@/assets/images/carousel/2.1before.jpg'),
          afterImage: require('@/assets/images/carousel/2.1after.jpg')
        },
        {
          beforeImage: require('@/assets/images/carousel/2.3before.jpg'),
          afterImage: require('@/assets/images/carousel/2.3after.jpg')
        }
      ]
    }
  },
  // computed属性已移除，现在使用动态的currentGalleryImages
  methods: {
    handleBack() {
      console.log('返回')
      // 处理返回逻辑
    },
    
    handleEdit() {
      console.log('编辑')
      // 处理编辑逻辑
    },
    
    handleTabChange(index) {
      this.activeTabIndex = index
      console.log('Tab changed:', index, this.tabData[index])
    },
    
    handleSlideChange(index) {
      console.log('Fan slide changed:', index)
      // 实现分类与价格的对应关系
      // 第一个分类（人像美容，索引0）对应5元（索引0）
      // 第二个分类（风景处理，索引1）对应15元（索引1）
      // 第三个分类（商品拍摄，索引2）对应30元（索引2）
      if (index >= 0 && index <= 2 && this.selectedPrice !== index) {
        this.selectedPrice = index
      }
    },
    
    handleSwiperSlideChange(index) {
      console.log('Swiper slide changed:', index)
    },
    
    handleSubmit(data) {
      console.log('提交:', data)
      // 处理提交逻辑
    },
    
    handleItemClick(data) {
      console.log('Fan 项点击:', data)
      const categoryIndex = data.index
      
      // 设置当前画廊显示的图片为该分类的所有图片
      this.currentGalleryImages = this.getCategoryGalleryImages(categoryIndex)
      
      // 默认显示该分类第一个项的处理后图片（奇数索引）
      this.galleryInitialIndex = 1
      this.galleryVisible = true
    },
    
    handleSwiperItemClick(data) {
      console.log('Swiper 项点击:', data)
      
      // 设置当前画廊显示的图片为普通轮播的所有图片
      this.currentGalleryImages = this.getSwiperGalleryImages()
      
      // 默认显示点击项的处理后图片（奇数索引）
      this.galleryInitialIndex = data.index * 2 + 1
      this.galleryVisible = true
    },
    
    handleGalleryClose() {
      this.galleryVisible = false
    },
    
    handleGalleryChange(index) {
      console.log('画廊图片变化:', index)
    },
    
    handleFanSwiperItemClick(data) {
      console.log('Fan Swiper 项点击:', data)
      const categoryIndex = data.fanIndex
      const swiperIndex = data.swiperIndex
      
      // 设置当前画廊显示的图片为该分类的所有图片
      this.currentGalleryImages = this.getCategoryGalleryImages(categoryIndex)
      
      // 显示点击的具体swiper项的处理后图片（奇数索引）
      this.galleryInitialIndex = swiperIndex * 2 + 1
      this.galleryVisible = true
    },
    
    handlePriceChange(priceIndex) {
      console.log('价格选择变化:', priceIndex)
      this.selectedPrice = priceIndex
      
      // 价格变化时，同步切换扇形轮播到对应的分类
      // 价格索引与扇形轮播分类索引一一对应
      if (this.$refs.fanCarousel && priceIndex >= 0 && priceIndex < this.carouselImages.length) {
        this.$refs.fanCarousel.goToSlide(priceIndex)
      }
    },
    
    // 获取swiperImages的画廊图片列表
    getSwiperGalleryImages() {
      const images = []
      this.swiperImages.forEach((item, index) => {
        images.push({
          src: item.beforeImage,
          alt: `普通轮播 - 第${index + 1}组 - 处理前`
        })
        images.push({
          src: item.afterImage,
          alt: `普通轮播 - 第${index + 1}组 - 处理后`
        })
      })
      return images
    },
    
    // 获取指定分类的画廊图片列表
    getCategoryGalleryImages(categoryIndex) {
      const images = []
      if (this.carouselImages[categoryIndex]) {
        const category = this.carouselImages[categoryIndex]
        category.items.forEach((item, itemIndex) => {
          images.push({
            src: item.beforeImage,
            alt: `${category.title} - 第${itemIndex + 1}组 - 处理前`
          })
          images.push({
            src: item.afterImage,
            alt: `${category.title} - 第${itemIndex + 1}组 - 处理后`
          })
        })
      }
      return images
    }
  }
}
</script>

<style scoped>
.app-container {
  max-width: 375px;
  margin: 0 auto;
  background: 
    radial-gradient(circle at 20% 80%, rgba(120, 80, 60, 0.3) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(160, 100, 70, 0.2) 0%, transparent 50%),
    radial-gradient(circle at 40% 40%, rgba(100, 60, 40, 0.2) 0%, transparent 50%),
    linear-gradient(135deg, #2D1B0E 0%, #1A0E08 40%, #0F0704 100%);
  min-height: 100vh;
  position: relative;
  overflow-x: hidden;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(25px) saturate(180%);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  position: fixed;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 375px;
  max-width: 100vw;
  z-index: 100;
  height: 56px;
}

.back-btn,
.edit-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  transition: all 0.2s ease;
  border-radius: 50%;
}

.back-btn:hover,
.edit-btn:hover {
  background: rgba(255, 255, 255, 0.15);
}

.back-btn:active,
.edit-btn:active {
  background: rgba(255, 255, 255, 0.25);
  transform: scale(0.95);
}

.tab-wrapper {
  flex: 1;
  overflow: hidden;
}

.swiper-carousel-section {
  padding: 88px 16px 24px;
  margin-bottom: 24px;
}

.carousel-section {
  padding: 0 0 24px;
  overflow: visible;
  margin-top: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.section-title {
  color: rgba(255, 255, 255, 0.9);
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 16px;
  text-align: center;
}

.custom-swiper-size .carousel-track {
  width: 300px;
  height: 200px;
}

.test-content {
  padding: 20px 16px 40px;
  color: rgba(255, 255, 255, 0.8);
}

.test-section {
  margin-bottom: 24px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 12px;
  padding: 16px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.test-section h3 {
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 12px;
}

.test-section ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.test-section li {
  padding: 8px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  font-size: 14px;
}

.test-section li:last-child {
  border-bottom: none;
}
</style>